﻿using SGArchiveSystem7x.App_Code;
using SGArchiveSystem7xDB.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers
{
    /// <summary>
    /// 主框架
    /// </summary>
    public class MainController : BaseController
    {
        ///[HttpPost] //只允许POST传递到此页面
        ///[ValidateAntiForgeryToken]//该注解可以防止跨站攻击
        ///[ActionName("Login")]//该注解可以更改路由中Action名称
        public ActionResult Index()
        {
            var n = this.VaildUser();
            if (n != null)
                return n;

            if (Models.Language._nowLanguage == SGArchiveSystem7xDB._VersionLanguage._Chs)
            {
                ViewBag.AppTitle = SGArchiveSystem7xCommons.SGWebInfo._sAppTitle;
                ViewBag.Weclome = "欢迎您";
                ViewBag.Text1 = "修改密码";
                ViewBag.Text2 = "重新登录";
                ViewBag.Text3 = "当前版本";
                ViewBag.Text4 = "系统模块";
                ViewBag.Text5 = "系统工作台";
                ViewBag.Text6 = "当前日期: " + System.DateTime.Now.ToString("yyyy 年 M 月 d 日");
                ViewBag.Current_version = "项目版本：" + SGArchiveSystem7xCommons.SGWebInfo._sAppVersion + "最后更新日期：" + SGArchiveSystem7xCommons.SGWebInfo._sAppLastUpdateTime;
                ViewBag.Text7 = "旧密码";
                ViewBag.Text8 = "新密码";
                ViewBag.Text9 = "密码的强度为：至少8位，含至少一个或多个大小写且含字母与数字的组合";
                ViewBag.Text10 = "密码长度不够";
                ViewBag.Text11 = "密码强度不够";
                ViewBag.Text12 = "旧密码不能为空";
                ViewBag.Text13 = "提示: 修改密码成功,请重新登录系统";
                ViewBag.Text14 = "提示: 修改失败";
                ViewBag.Text15 = "提示: 旧密码错误";
                ViewBag.Text16 = "两次密码输入不一致";
                ViewBag.Text17 = "你确定退出重新登录吗?";
            }
            else
            {
                ViewBag.AppTitle = SGArchiveSystem7xCommons.SGWebInfo._sAppTitle_Eng;
                ViewBag.Weclome = "Welcome";
                ViewBag.Text1 = "Change Password";
                ViewBag.Text2 = "Re login";
                ViewBag.Text3 = "Now Version";
                ViewBag.Text4 = "System Module";
                ViewBag.Text5 = "Workbench";
                ViewBag.Text6 = "Current Date: " + System.DateTime.Now.ToString("yyyy - M - d");
                ViewBag.Current_version = "Version：" + SGArchiveSystem7xCommons.SGWebInfo._sAppVersion + "Last update date：" + SGArchiveSystem7xCommons.SGWebInfo._sAppLastUpdateTime;
                ViewBag.Text7 = "Old Password";
                ViewBag.Text8 = "New Password";
                ViewBag.Text9 = "The strength of a password is at least 8 bits, including at least one or more case letters and combinations of letters and numbers";
                ViewBag.Text10 = "Password length is not enough";
                ViewBag.Text11 = "Password strength is not enough";
                ViewBag.Text12 = "Old passwords cannot be empty";
                ViewBag.Text13 = "Tip: The password was changed successfully, please log in again";
                ViewBag.Text14 = "Tip: Change failed";
                ViewBag.Text15 = "Tip: Old password error";
                ViewBag.Text16 = "Two passwords are inconsistent";
                ViewBag.Text17 = "Are you sure you want to log out and log in again?";
            }

            var list = SGArchiveSystem7x.Models.CMenuModel.CreateModel();
            if (list == null)
                return ToErrPage();

            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);

            ViewBag.UName = user.U_RNAME;
            ViewBag.UID = user.U_ID;
            ViewBag.BmName = user.ROLE_NAME;
            ViewBag.RMD = user.ROLE_V01.Split(',');

          
            ViewBag.Menu = list;

            var bm = SGArchiveSystem7xDB.Linq.T_DeparetmentDal.Get_Department(user.U_DEPID.Value);
            SGArchiveSystem7x.App_Code.CSession.Add_Bm(Session, bm);
            ViewBag.BM = bm;

            var en = SGArchiveSystem7xDB.Linq.T_SyspzDal.Get_SysPZ("NAME");
            //if (en == null || en.PZ_INFO == "")
            //    ViewBag.Title = "中卡RFID仓储管理系统标准版"; //"数字档案室库房RFID电子标签管理系统";
            //else
            //    ViewBag.Title = en.PZ_INFO;

            /////如果上次修改密码的时间已经超过1个月了
            //if(user.U_D02 != null)
            //{
            //    DateTime dt = DateTime.Now;
            //    if (dt.Subtract(user.U_D02.Value).Days > 30)
            //    {
            //        Session.Add("SGUserPwd", "0");
            //        return RedirectToAction("ChangePwd", "Main");
            //    }
            //}

            return View();
        }

        /// <summary>
        /// 没有权限
        /// </summary>
        /// <returns></returns>
        public ActionResult NoAuthority()
        {
            ///没有权限显示此页面
            return View();
        }

        /// <summary>
        /// 404 页面显示
        /// </summary>
        public ActionResult To404Page()
        {
            return View();
        }

        /// <summary>
        /// SESSION 丢失页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ToSessionErrPage()
        {
            return View();
        }
        /// <summary>
        /// 系统错误 页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ToErrPage()
        {
            return View();
        }
        /// <summary>
        /// 重新登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string JumpLogin()
        {
            SGArchiveSystem7x.App_Code.CSession.Clear_AllSession(Session);
            return "1";
        }


        [HttpPost]
        public string btnSavePassword(string old, string pwd)
        {
            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);
            if (user == null)
                //session过期 
                return "-99";
            else
            {
                if (old != "")
                {
                    var value = (int)T_UserDal.Modify_Pwd(user.U_ID, old, pwd);
                    return value.ToString();
                }
                else
                {
                    var value = (int)T_UserDal.Modify_Pwd(user.U_ID, pwd);
                    return value.ToString();
                }
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangePwd(string type)
        {
            var n = this.VaildUser();
            if (n != null)
                return n;

            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);

            ViewBag.UName = user.U_RNAME;
            ViewBag.UID = user.U_ID;
            ViewBag.BmName = user.ROLE_NAME;

            var bm = SGArchiveSystem7xDB.Linq.T_DeparetmentDal.Get_Department(user.U_DEPID.Value);
            SGArchiveSystem7x.App_Code.CSession.Add_Bm(Session, bm);
            ViewBag.BM = bm;

            string value = "1";
            if (Session["SGUserPwd"] != null)
                value = Session["SGUserPwd"].ToString();

            if (value == "0")
            {
                ViewBag.vi = "密码时间过期";
                ViewBag.info = "您距离上次修改密码已经超过30天了，必须重新指定新的密码，密码的强度为：至少8位，含至少一个或多个大小写且含字母与数字的组合";
            }
            else if(value == "1")
            {
                ViewBag.vi = "初始密码修改";
                ViewBag.info = "您现在的密码时候初始密码，必须修改密码后才能登录系统，密码的强度为：至少8位，含至少一个或多个大小写且含字母与数字的组合";
            }
            //else if(value == "2")

            if(type == "1")
            {
                ViewBag.vi = "密码复杂度修改";
                ViewBag.info = "您现在的密码过于简单，必须修改密码后才能登录系统，密码的强度为：至少8位，含至少一个或多个大小写且含字母与数字的组合";
            }
            return View();
        }

        /// <summary>
        /// 获取当前信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Get_ajaxWorkInfo()
        {
            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);
            var listInts = SGArchiveSystem7xDB.Linq.T_BInfoDal.Get_BrrSPInfo(user.U_ID);

            string html = "";
            if (listInts[0] > 0)
                html += "电子借阅:&nbsp;<span style='color:yellow;font-size:14px'>&nbsp;" + listInts[0] + "</span>&nbsp;&nbsp;";
            if (listInts[1] > 0)
                html += "档案出库:&nbsp;<span style='color:yellow'>&nbsp;" + listInts[1] + "</span>&nbsp;&nbsp;";
            if (listInts[2] > 0)
                html += "申请等待审批的:&nbsp;<span style='color:yellow'>&nbsp;" + listInts[2] + "</span>&nbsp;&nbsp;";
            if (listInts[3] > 0)
                html += "需要您审批的:&nbsp;<span style='color:yellow'>&nbsp;" + listInts[3] + "</span>&nbsp;&nbsp;";

            if (html != "")
                html = "您目前档案工作情况: " + html;
            //string html = "<li><a href = '#'><i class='icon-user-plus'></i>电子借阅: " + listInts[0] + "</a></li>" +
            //            "<li><a href = '#'><i class='icon-switch2'></i>纸质借阅: " + listInts[1] + "</a></li>" +
            //            "<li><a href = '#'><i class='icon-user'></i>申请还未审批: " + listInts[2] + "</a></li>" +
            //            "<li><a href = '#'><i class='icon-user'></i>需要您审批的: " + listInts[3] + "</a></li>";
            return html;
        }
    }
}